﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;
using Joyintech.Common;
using System.Web.SessionState;

namespace Joyintech.Web.inpiao365.PhenixTicket
{
    /// <summary>
    /// longin 的摘要说明
    /// </summary>
    public class longin : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            String name = context.Request["name"].Trim();
            String psw = context.Request["psw"].Trim();
            string result = Login(name, psw);
            context.Response.Write(result);
        }

        protected string Login(string txtUserName, string txtPwd)
        {
            string strError = string.Empty;

            if (txtUserName == "")
            {
                strError += "用户名不可为空";
            }

            if (txtPwd == "")
            {
                strError += "密码不可为空";
            }
            if (txtUserName != "" && txtPwd != "")
            {
                customer model = new customer();

                model = customer_BLL.GetInstance().GetModel(txtUserName);

                if (model != null && model.CustomerPass == Utils.MD5(txtPwd))
                {
                    //更新最后登录的时间
                    customer newModel = model;
                    newModel.CustomerLastLoginTime = DateTime.Now;
                    customer_BLL.GetInstance().Update(newModel);

                    //向Cookies中存储SessionId
                    customer_BLL.GetInstance().StoreSessionId(HttpContext.Current.Session.SessionID);

                    //向Memcached中存储SessionId对应的当前登录会员实体
                    customer_BLL.GetInstance().StoreCurrentCustomer(HttpContext.Current.Session.SessionID, model);

                    //根据用户居住地获取用户的城市Id
                    List<movieCity> listMovieCity = new movieCity_BLL().GetModelList("CityName='%" + model.CustomerResidence + "%'");

                    if (listMovieCity != null && listMovieCity.Count > 0)
                    {
                        //将用户所属城市id写入Cookies，名称为“ztyingonde”                
                        Utils.WriteCookie("ztyingonde", listMovieCity[0].CityId.ToString());
                    }
                }
                else
                {
                    strError += "用户名或密码错误，请重新输入！";
                }
            }
            return strError;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}